package com.miracle.main.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.miracle.main.R;

/**
 * com.miracle.main.util
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/12 13:31
 * @see com.miracle.main.util
 */
public class PhotoSelectPopup extends PopupWindow implements View.OnClickListener {

    private TextView btnTakePhoto, btnSelect, btnCancel;
    private View mPopView;
    private OnItemClickListener mListener;

    private Context mContext;

    public PhotoSelectPopup(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mContext = context;
        init(context);
        setPopupWindow();
        btnTakePhoto.setOnClickListener(this);
        btnSelect.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(context);
        //绑定布局
        mPopView = inflater.inflate(R.layout.popup_select_camera, null);

        btnTakePhoto = mPopView.findViewById(R.id.tv_xc);
        btnSelect = mPopView.findViewById(R.id.tv_xj);
        btnCancel = mPopView.findViewById(R.id.tv_cancel);
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        //0.0-1.0
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    /**
     * 设置窗口的相关属性
     */
    @SuppressLint("InlinedApi")
    private void setPopupWindow() {
        // 设置View
        this.setContentView(mPopView);
        // 设置弹出窗口的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置弹出窗口的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置弹出窗口可
        this.setFocusable(true);
        // 设置动画
        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        // 设置背景透明
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));
    }

    /**
     * 定义一个接口，公布出去 在Activity中操作按钮的单击事件
     */
    public interface OnItemClickListener {
        /**
         * 点击
         * @param v
         */
        void setOnItemClick(View v);
    }

    /**
     * 点击事件
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (mListener != null) {
            mListener.setOnItemClick(v);
        }
    }

}
